package com.oujia.cloud.cloudcommon.redis;


import com.oujia.cloud.cloudcommon.api.APIResultEnum;
import com.oujia.cloud.cloudcommon.bean.config.RedisManager;
import com.oujia.cloud.cloudcommon.bean.exception.ServiceException;
import com.oujia.cloud.cloudcommon.util.AESUtils;
import com.oujia.cloud.cloudcommon.util.StringUtils;

public class RedisKey {
    //用户token
    public final static String USER_TOKEN = "user:token:";
    public final static String USER_NAME_TOKEN = "user:name:";
    //oauth2.0 token
    public final static String OAUTH_ACCESS_TO_REFRESH = "access_to_refresh:";
    public final static String OAUTH_REFRESH = "refresh:";
    public final static String OAUTH_REFRESH_AUTH = "refresh_auth:";
    public final static String OAUTH_REFRESH_TO_ACCESS = "refresh_to_access:";
    public final static String USER_WX_OPNID_OLD = "user:wx:openId:old:";
    //邀请数据伪造
    public final static String USER_INVITATION_WZ = "user:invitation:wz:";
    //累计分享数据
    public final static String USER_INVITATION_SIZE = "user:invitation:size";

    //用户登录微信参数
    public final static String USER_WX_PRAM = "user:wx:pram:";

    //用户验证码
    public final static String USER_REGISTER_CODE = "user:register:code:";

    //用户邀请验证码
    public final static String USER_INVITATION_CODE = "user:invitation:code:";

    //用户邀请码图片地址
    public final static String USER_INVITATION_PICTURE = "user:invitation:picture";
    //用户提现同步锁
    public final static String SYNCHRONIZED_USER_TX = "synchronized:user:tx";
    //用户邀请同步锁
    public final static String SYNCHRONIZED_USER_INVITATION = "synchronized:user:invitation";
    //用户确认提现同步锁
    public final static String SYNCHRONIZED_USER_ADMIN_TX = "synchronized:user:adminTx";

    //用户退款
    public final static String SYNCHRONIZED_USER_ADMIN_ORDER = "synchronized:user:adminOrder";
    //待发送的消息
    public final static String MSG_TIME_START = "msg:time:start";
    //待结束的消息
    public final static String MSG_TIME_FINISH = "msg:time:finish";
    //推送定时器同步锁
    public final static String MSG_TIME_PUSH = "msg:time:push";
    //用户推送标签集合
    public final static String USER_PUSH_TAG = "user:push:tag:";
    //推送标签集合
    public final static String USER_PUSH_TAGS = "user:push:tags";
    //用户搜索标签集合
    public final static String USER_SEARCH_HISTORY = "user:search:history:";
    //jd访问token
    public final static String JD_ACCESS_TOKEN = "jd:access:token";
    //jd刷新token
    public final static String JD_REFRESH_TOKEN = "jd:refresh:token";
    //book md5
    public final static String BOOK_MD5 = "book:md5";
    public static UserToken getUser(String token) {
        UserToken userToken = RedisManager.get(RedisKey.USER_TOKEN + token, UserToken.class);
        if (userToken != null) {
            return userToken;
        } else {
            throw new ServiceException(APIResultEnum.TOKEN_UNIODID);
        }
    }

    public static Long getUserId(String token) {
        if (StringUtils.isNotBlank(token)) {
            UserToken userToken = RedisManager.get(RedisKey.USER_TOKEN + token, UserToken.class);
            if (userToken != null) {
                return userToken.getUserId();
            } else {
                throw new ServiceException(APIResultEnum.TOKEN_UNIODID);
            }
        }
        return null;
    }

    public static void getCmsToken(String token, String jwtSecret) {
        String userLoginVO = AESUtils.decrypt2str(token, jwtSecret);
        if (StringUtils.isBlank(userLoginVO)) {
            throw new ServiceException(APIResultEnum.CMSTOKEN_ERR);
        }
    }

}
